//
//  HSPageCotrol.swift
//  HSMainViewControllerHandler
//
//  Created by haisheng huang on 15/12/29.
//  Copyright © 2015年 haisheng huang. All rights reserved.
//

import UIKit
import Foundation

@objc
public enum HSPageControlType: Int {
    case middle
    case leftBottom
    case rightBottom
    case none
}


@available(iOS 7.0, *)
@objc open class HSPageControl: UIPageControl {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    public convenience init(type: HSPageControlType, superView: UIView, numberOfPages: Int, currentPage: Int) {
        
        self.init()
        self.numberOfPages = numberOfPages
        self.currentPage = currentPage
        
        if self.constraints.count == 0 {
            
            let size: CGSize = self.size(forNumberOfPages: numberOfPages)
            let frame: CGRect = self.getFrame(type, superFrame: superView.frame, size: size)
            self.frame = frame
            superView.addSubview(self)
            
        } else {
            
            self.addPageControlConstraints(type, superView: superView)

        }
        
    }
    
    fileprivate func getFrame(_ type: HSPageControlType, superFrame: CGRect, size: CGSize) -> CGRect {
        
        var frame: CGRect = CGRect.zero
        frame.size = size
        var point: CGPoint = CGPoint(x: 0, y: superFrame.height - size.height)
        
        if type == .middle {
            point.x = (superFrame.width - size.width)/2
        } else if type == .leftBottom {
            point.x = 15.0
        } else if type == .rightBottom {
            point.x = superFrame.width - size.width - 15.0
        }
        frame.origin = point
        return frame
        
    }
    
    open func addPageControlConstraints(_ type: HSPageControlType, superView: UIView) {
        
        let size: CGSize = self.size(forNumberOfPages: self.numberOfPages)
        //print("size width : \(size.width)")
        
        superView.addSubview(self)
        
        self.translatesAutoresizingMaskIntoConstraints = false
        
        self.addConstraint(NSLayoutConstraint.init(item: self, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: size.width))
        
        self.addConstraint(NSLayoutConstraint.init(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: size.height))
        
        superView.addConstraint(NSLayoutConstraint.init(item: self, attribute: .bottom, relatedBy: .equal, toItem: superView, attribute: .bottom, multiplier: 1, constant: 1))
        
        if type == .middle {
            superView.addConstraint(NSLayoutConstraint.init(item: self, attribute: .centerX, relatedBy: .equal, toItem: superView, attribute: .centerX, multiplier: 1, constant: 0))
        } else if type == .leftBottom {
            superView.addConstraint(NSLayoutConstraint.init(item: self, attribute: .left, relatedBy: .equal, toItem: superView, attribute: .left, multiplier: 1, constant: 15))
        } else if type == .rightBottom {
            superView.addConstraint(NSLayoutConstraint.init(item: self, attribute: .right, relatedBy: .equal, toItem: superView, attribute: .right, multiplier: 1, constant: -15))
        }
        
        
    }
    
}
